//
//  MOImageTextView.m
//  AFBrowser
//
//  Created by alfie on 2021/1/25.
//

#import "MOImageTextView.h"
#import <Masonry/Masonry.h>
#import "QAFriendModel.h"
#import "QAConversation.h"

@interface MOImageTextView ()

/** 文本 */
@property (nonatomic, strong) UILabel            *titleLb;

/** 图片 */
@property (nonatomic, strong) UIImageView        *imageView;

/** 图片1 */
@property (nonatomic, strong) UIImageView        *imageView1;

/** 图片的size */
@property (nonatomic, assign) CGSize            imageSize;

/** 图片和文本的间距 */
@property (nonatomic, assign) CGFloat           titleSpaceImage;

/** 最大宽度 */
@property (nonatomic, assign) CGFloat           maxWidth;

/** 图片 */
@property (nonatomic, strong) UIImage           *image;

/** 是否需要绘制 */
@property (nonatomic, assign) BOOL             needsDisplay;

@property (nonatomic, assign) BOOL             needsAppendWidth;

@end
  

@implementation MOImageTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = NO;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]];
    }
    return self;
}


#pragma mark - Getter
- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = UILabel.new;
        //_titleLb.localizedTextEnable = NO;
        [self addSubview:_titleLb];
        [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
    }
    return _titleLb;
}


- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = UIImageView.new;
        [self addSubview:_imageView];
    }
    return _imageView;
}

- (UIImageView *)imageView1 {
    if (!_imageView1) {
        _imageView1 = UIImageView.new;
        [self addSubview:_imageView1];
    }
    return _imageView1;
}
#pragma mark - setter
- (void)setDelegate:(id<MOImageTextViewDelegate>)delegate {
    _delegate = delegate;
    if (delegate) {
        self.userInteractionEnabled = YES;
    }
}

- (void)setFont:(UIFont *)font {
    _font = font;
    self.titleLb.font = font;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.titleLb.textColor = textColor;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment {
    _textAlignment = textAlignment;
    self.titleLb.textAlignment = textAlignment;
}

- (void)setNumberOfLines:(NSInteger)numberOfLines {
    _numberOfLines = numberOfLines;
    self.titleLb.numberOfLines = numberOfLines;
}

- (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode {
    _lineBreakMode = lineBreakMode;
    self.titleLb.lineBreakMode = lineBreakMode;
}

- (void)setText:(NSString *)text {
    _text = text;
    self.titleLb.text = text;
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    _attributedText = attributedText;
    self.titleLb.attributedText = attributedText;
}

- (void)setIsShowNomal:(BOOL)isShowNomal {
    _isShowNomal = isShowNomal;
    
}

#pragma mark - 绑定数据
- (void)attachText:(NSString *)text image:(UIImage *)image titleSpaceImage:(CGFloat)titleSpaceImage imageSize:(CGSize)imageSize maxWidth:(CGFloat)maxWidth {
    
    self.titleLb.text = text;

    CGFloat width = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 25)
                                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                        attributes:@{NSFontAttributeName : self.titleLb.font}
                                       context:nil].size.width;
    width = ceil(width);
    if (image && !self.isShowNomal) {
        self.imageView.image = image;
        CGFloat maxTitle_W = maxWidth - imageSize.width - titleSpaceImage;
        if (width > maxTitle_W) {
            width = maxTitle_W;
            titleSpaceImage = 2;
        }
        [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.offset(0);
            make.width.offset(width);
        }];
        
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
              
            make.left.equalTo(self.titleLb.mas_right).offset(titleSpaceImage);
            make.centerY.offset(0);
            make.width.offset(imageSize.width);
            make.height.offset(imageSize.height);
            make.right.offset(0).priorityMedium();
        }];
        
    } else {
        if (width > maxWidth) {
            width = maxWidth;
        }
        if (_imageView.superview) {
            [_imageView removeFromSuperview];
            _imageView = nil;
        }
        [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.offset(0);
            make.width.offset(width);
            make.right.mas_lessThanOrEqualTo(0).priorityMedium();
        }];
    }
}

- (void)handleKgroupName:(QAConversation *)group maxWidth:(CGFloat)maxWidth{
    if (_imageView.superview) {
        [_imageView removeFromSuperview];
        _imageView = nil;
    }
    if (_imageView1.superview) {
        [_imageView1 removeFromSuperview];
        _imageView1 = nil;
    }
    
    self.titleLb.text = group.groupName;

    CGFloat width = [group.groupName boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 25)
                                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                        attributes:@{NSFontAttributeName : self.titleLb.font}
                                       context:nil].size.width;
    width = ceil(width);
    if (self.isShowNomal) {
        if (width > maxWidth) {
            width = maxWidth;
        }
        [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.offset(0);
            make.width.offset(width);
            make.right.mas_lessThanOrEqualTo(0).priorityMedium();
        }];
    }else{
        UIImage *kgroupImage = [UIImage imageNamed:@"im_group_kGroup_icon"];
        UIImage *vipImage = group.isSuperGroup ? [UIImage imageNamed:@"superGroup_icon_im_group"] : nil;
        CGSize kGroupImageSize = CGSizeMake(15, 14);
        CGSize imageSize = CGSizeMake(38, 14);
        CGFloat titleSpaceImage = 5;
        CGFloat maxTitle_W = maxWidth;
        //知识群Tag
        self.imageView1.image = kgroupImage;
        maxTitle_W = maxTitle_W - kGroupImageSize.width - titleSpaceImage;
        if (width > maxTitle_W) {
            width = maxTitle_W;
            titleSpaceImage = 2;
        }
        [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.offset(0);
            make.width.offset(width);
        }];
        [self.imageView1 mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLb.mas_right).offset(titleSpaceImage);
            make.centerY.offset(0);
            make.width.offset(kGroupImageSize.width);
            make.height.offset(kGroupImageSize.height);
            make.right.offset(0).priorityMedium();
        }];
        //超级群Tag
        if (vipImage) {
            self.imageView.image = vipImage;
            maxTitle_W = maxTitle_W - imageSize.width - titleSpaceImage;
            if (width > maxTitle_W) {
                width = maxTitle_W;
                titleSpaceImage = 2;
            }
            [self.titleLb mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.offset(0);
                make.width.offset(width);
            }];
            
            [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.imageView1.mas_right).offset(titleSpaceImage);
                make.centerY.offset(0);
                make.width.offset(imageSize.width);
                make.height.offset(imageSize.height);
                make.right.offset(0).priorityMedium();
            }];
        }
    }
}

#pragma mark - 显示昵称特殊展示
- (void)displayWithData:(id)data maxWidth:(CGFloat)maxWidth {
    if (_imageView1.superview) {
        [_imageView1 removeFromSuperview];
        _imageView1 = nil;
    }
    NSString *title;
    if ([data isKindOfClass:QAUserInfoModel.class]) {
        QAUserInfoModel *model = data;
        title = model.displayNickname;
    } else if ([data isKindOfClass:QAFriendModel.class]) {
        QAFriendModel *model = (QAFriendModel *)data;
        title = model.collationGroupString;
    } else if ([data isKindOfClass:QAConversation.class]) {
        QAConversation *model = (QAConversation *)data;
        if (model.isGroup) {
            // 群聊
            /// 普通群
            [self attachText:model.groupName image:nil titleSpaceImage:0 imageSize:CGSizeZero maxWidth:maxWidth];
        } else {
            // 取单聊数据
            [self displayWithData:model.friendModel maxWidth:maxWidth];
        }
        return;
    } else {
        return;
    }
    
    [self attachText:title image:nil titleSpaceImage:0 imageSize:CGSizeZero maxWidth:maxWidth];
}


#pragma mark - 点击事件
- (void)tapAction {
    if ([self.delegate respondsToSelector:@selector(didSelectedImageTextView:)]) {
        [self.delegate didSelectedImageTextView:self];
    }
}



@end
